package com.makefuture.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 使用poi报表导出工具类
 * 把poi的一个调用接口抽出来，便于导出功能的管理
 * @author LiangXianYou
 * @param 
 *
 */
public class ExcelUtil {
	
	/**
	 * <b>导出list中map做载体的数据到excel</b><br>
	 * 参数说明：<br>
	 * 	list:存放了Map数据的集合<br>
	 * 	hdNames:表头列名<br>
	 * 	hds:对应表头的数据KEY<br>
	 *  xlsName:导出文件名<br>
	 * @author LiangXianYou
	 * @date 2015-6-25 上午10:22:51
	 */
	public static <T> boolean outPutExcelByMap(List<Map<String, Object>> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Workbook wb = new HSSFWorkbook();			//创建工作薄
		Sheet sheet = wb.createSheet();				//创建工作表
		sheet.autoSizeColumn(( short ) 0 );			//自适应宽度
		//写入表头---Excel的第一行数据
		Row nRow = sheet.createRow(0);				//创建行
		for(int i=0;i<hdNames.length;i++){
			Cell nCell = nRow.createCell(i);			//创建单元格
			nCell.setCellValue(hdNames[i]);
		}  
		
		//写入每一行数据---一条记录就是一行数据
		for(int i=0;i<list.size();i++){
			for(int j=0;j<hds.length;j++){
				Object o = list.get(i).get(hds[j]);		//得到列的值
				data2Excel(sheet, o, i+1, j);			//将值写入Excel
			}
		}
		return downloadExcel(wb, xlsName, request, response);
	}

	/**
	 * Excels导出多个sheet
	 * @author LiangXianYou
	 * @date 2015-6-16 下午5:56:56
	 */
	//map:data,sheetName,hds,hdNames,xlsName;request,response
	public static <T> boolean outPutExcels(List<Map<String, Object>> list,String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{

		Workbook wb = new HSSFWorkbook();			//创建工作薄
		for(int i=0;i<list.size();i++){
			Map<String, Object> map = list.get(i);
			Sheet sheet = wb.createSheet();				//创建工作表
			String sheetName = (String) map.get("sheetName");
			wb.setSheetName(i, sheetName);
			//写入表头---Excel的第一行数据
			Row nRow = sheet.createRow(0);				//创建行
			String[] hdNames = (String[]) map.get("hdNames");
			for(int j=0;j<hdNames.length;j++){
				Cell nCell = nRow.createCell(j);			//创建单元格
				nCell.setCellValue(hdNames[j]);
			}
			
			//写入每一行数据---一条记录就是一行数据
			@SuppressWarnings("unchecked")
			List<T> data = (List<T>) map.get("data");
			String[] hds = (String[]) map.get("hds");
			for(int j=0;j<data.size();j++){
				for(int k=0;k<hds.length;k++){
					Object o = getFieldValue(data.get(j), hds[k]);	//得到列的值
					data2Excel(sheet, o, j+1, k);					//将值写入Excel
				}
			}
		}
		
		return downloadExcel(wb, xlsName, request, response);
	}
	
	/**
	 * 参数说明：
	 * list：list数据集合
	 * hdNames：表头需要显示的名称
	 * hds：表头对应的对象属性名称，和 hdNames一一对应
	 * xlsName：导出Excel的预定义名称
	 * request:	HttpServletRequest
	 * response：HttpServletResponse
	 * 通过数据构建Excel
	 * @author LiangXianYou
	 * @throws Exception 
	 * @date 2015-3-13 上午11:00:30
	 */
	public static <T> boolean outPutExcel(List<T> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Workbook wb = new HSSFWorkbook();			//创建工作薄
		Sheet sheet = wb.createSheet();				//创建工作表
		sheet.autoSizeColumn(( short ) 0 );			//自适应宽度
		//写入表头---Excel的第一行数据
		Row nRow = sheet.createRow(0);				//创建行
		for(int i=0;i<hdNames.length;i++){
			Cell nCell = nRow.createCell(i);			//创建单元格
			nCell.setCellValue(hdNames[i]);
		}
		
		//写入每一行数据---一条记录就是一行数据
		for(int i=0;i<list.size();i++){
			for(int j=0;j<hds.length;j++){
				Object o = getFieldValue(list.get(i), hds[j]);	//得到列的值
				data2Excel(sheet, o, i+1, j);						//将值写入Excel
			}
		}
		return downloadExcel(wb, xlsName, request, response);
	}
	
	/**
	 * 传递一个Wookbook，给定文件名，以及request和response下载Excel文档 
	 * @author LiangXianYou
	 * @throws IOException
	 * @date 2015-3-13 上午9:00:36
	 */
	@SuppressWarnings("all")
	private static boolean downloadExcel(Workbook wb, String xlsName, HttpServletRequest request, HttpServletResponse response) throws IOException{
			if(request.getHeader("user-agent").indexOf("MSIE") != -1) {   
				xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls"; 
			} else {
			    xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls";   
			}
			OutputStream os = response.getOutputStream();
			response.setContentType("application/vnd.ms-excel");    
			response.setHeader("Content-disposition", "attachment;filename="+xlsName);    
			
			wb.write(os);
			return true;
	}
	
	/**
	 * 通过泛型实例对象得到某一字段值
	 * @author LiangXianYou
	 * @date 2015-3-13 上午10:53:32
	 */
	private static <T> Object  getFieldValue(T t, String fieldName) throws Exception{
		if(t == null){
			return null;
		}
		Field field = t.getClass().getDeclaredField(fieldName);
		field.setAccessible(true);//暴力反射
		Object o = field.get(t);//得到字段数据的值
		return o;
	}
	
	/**
	 * 将数据写到Excel中
	 * @author LiangXianYou
	 * @date 2015-3-13 上午10:37:55
	 */
	private static void data2Excel(Sheet sheet, Object o, Integer r, Integer c){
		//通过获得sheet中的某一列，有得到，没有创建
		Row nRow = sheet.getRow(r);
		if(nRow == null){
			nRow = sheet.createRow(r);
		}
//		nRow.setColumnWidth(r, arg1);
		
		Cell nCell = nRow.createCell(c);
		
		
		//根据不同类型进行转化，如有其它类型没有考虑周全的，使用发现的时候添加
		char type = 'x';
		if(o instanceof Integer){
			type = 1;
		}else if(o instanceof Double){
			type = 2;
		}else if(o instanceof Float){
			type = 3;
		}else if(o instanceof String){
			type = 4;
		}else if(o instanceof Date){
			type = 5;
		}else if(o instanceof Calendar){
			type = 6;
		}else if(o instanceof Boolean){
			type = 7;
		}else if(o == null) {
			type = 8;
		}
		
		switch (type) {
			case 1:nCell.setCellValue((Integer)o);break;
			case 2:nCell.setCellValue((Double)o);break;
			case 3:nCell.setCellValue((Float)o);break;
			case 4:nCell.setCellValue((String)o);break;
			case 5:nCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o));break;
			case 6:nCell.setCellValue((Calendar)o);break;
			case 7:nCell.setCellValue((Boolean)o);break;
			case 8:nCell.setCellValue("");break;
			default:nCell.setCellValue(o + "");break;
		}
	}
	
	public static String[][] getExcelData(File file, int ignoreRows)
			throws FileNotFoundException, IOException {
		List result = new ArrayList();
		int rowSize = 0;
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(
				file));
		// 打开HSSFWorkbook
		POIFSFileSystem fs = new POIFSFileSystem(in);
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		HSSFCell cell = null;
		for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
			HSSFSheet st = wb.getSheetAt(sheetIndex);

			// 第一行为标题，不取
			for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {
				HSSFRow row = st.getRow(rowIndex);
				if (row == null) {
					continue;
				}
				int tempRowSize = row.getLastCellNum() + 1;
				if (tempRowSize > rowSize) {
					rowSize = tempRowSize;
				}
				String[] values = new String[rowSize];
				Arrays.fill(values, "");
				boolean hasValue = false;
				for (int columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
					String value = "";
					cell = row.getCell(columnIndex);
					if (cell != null) {
						// 注意：一定要设成这个，否则可能会出现乱码
						// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
						switch (cell.getCellType()) {
						case HSSFCell.CELL_TYPE_STRING:
							value = cell.getStringCellValue();
							break;
						case HSSFCell.CELL_TYPE_NUMERIC:
							if (HSSFDateUtil.isCellDateFormatted(cell)) {
								Date date = cell.getDateCellValue();
								if (date != null) {
									value = new SimpleDateFormat("yyyy-MM-dd")
											.format(date);
								} else {
									value = "";
								}
							} else {
								value = new BigDecimal(cell.getNumericCellValue()).toString();  
								if (value.indexOf(".") != -1) {
									DecimalFormat df = new DecimalFormat(
											"0.000000");
									value = df.format(cell
											.getNumericCellValue());
								} else {
									DecimalFormat df = new DecimalFormat("0");
									value = df.format(cell
											.getNumericCellValue());
								}
							}
							break;
						case HSSFCell.CELL_TYPE_FORMULA:
							// 导入时如果为公式生成的数据则无值
							if (!cell.getStringCellValue().equals("")) {
								value = cell.getStringCellValue();
							} else {

								value = cell.getNumericCellValue() + "";
							}
							break;
						case HSSFCell.CELL_TYPE_BLANK:
							break;
						case HSSFCell.CELL_TYPE_ERROR:
							value = "";
							break;
						case HSSFCell.CELL_TYPE_BOOLEAN:
							value = (cell.getBooleanCellValue() == true ? "Y"
									: "N");
							break;
						default:
							value = "";
						}

					}

					if (columnIndex == 0 && value.trim().equals("")) {
						break;
					}

					values[columnIndex] = rightTrim(value);
					hasValue = true;
				}

				if (hasValue) {

					result.add(values);

				}

			}

		}

		in.close();

		String[][] returnArray = new String[result.size()][rowSize];
         for (int i = 0; i < returnArray.length; i++) {
         	returnArray[i] = (String[]) result.get(i);
         }
        return returnArray;

	}
	/**
	 * 
	 * 去掉字符串右边的空格
	 * 
	 * @param str
	 *            要处理的字符串
	 * 
	 * @return 处理后的字符串
	 */

	public static String rightTrim(String str) {
		if (str == null) {
			return "";
		}
		int length = str.length();
		for (int i = length - 1; i >= 0; i--) {
			if (str.charAt(i) != 0x20) {
				break;
			}
			length--;
		}
		return str.substring(0, length);
	}

	public static String getNumeric(String str) {
		str = str.trim();
		Pattern pattern = Pattern.compile("\\d+\\.\\d+$|-\\d+\\.\\d+$");// 判断是否为小数
		Matcher isNum = pattern.matcher(str);
		if (isNum.matches()) {
			DecimalFormat df = new DecimalFormat("0");
			return df.format(Float.parseFloat(str));
		}
		return str;

	}
   
	   /**
	    * 导入测试
	    * @param args
	    */
		public static void main(String[] args) {
			String[][] exceldata=null;
			File file = new File("test.xls");
			try {
				exceldata=ExcelUtil.getExcelData(file, 0);//0从第一行开始,如果要从第二行开始遍历,就写1.如果要从第三行开始遍历,就写2.
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(null!=exceldata){
				for (int i = 0; i < exceldata.length; i++) {
					String [] rowdata=exceldata[i];
					String lie1Date = rowdata[0];
					String lie2Date = rowdata[1];
					String lie3Date = rowdata[2];
					String lie4Date = rowdata[3];
					String lie5Date = rowdata[4];
					String lie6Date = rowdata[5];
					String lie7Date = rowdata[6];
					System.out.println("第"+(i+1)+"行,  第一列的值:"+lie1Date+" , 第二列的值:"+lie2Date+" , 第三列的值:"
					+lie3Date+" , 第四列的值:"+lie4Date+" , 第五列的值:"+lie5Date+" , 第六列的值:"+lie6Date+" , 第七列的值:"+lie7Date);
				}
		}
	}
}
